﻿using Client.Constant;
using Client.Framework.Message;
using Client.Model;
using Client.Service;
using Client.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.Message.Response
{
    [MessageAttr(MessageId.USER_LIST)]
    class UserListResp : ResponseBaseMessage
    {
        public List<ViewUser> Users { get; } = new List<ViewUser>();
        public int Count { get; set; }

        public override void Deserialize(byte[] buffer)
        {
            UserList msg = UserList.Parser.ParseFrom(buffer);
            foreach (var user in msg.Users)
            {
                ViewUser viewUser = new ViewUser
                {
                    ID = user.Id,
                    OrgId = user.OrgId,
                    OrgName = OrgService.Instance().GetOrgById(user.OrgId) == null ? string.Empty : OrgService.Instance().GetOrgById(user.OrgId).OrgName,
                    LoginName = user.LoginName,
                    Password = MD5Util.MD5Decrypt(user.Password),
                    Status = (Enum.DataStatus)user.Status,
                    Sort = user.Sort,
                    Creator = user.Creator,
                    CreateTime = user.CreateTime,
                    Updator = user.Updator,
                    UpdateTime = user.UpdateTime,
                    // 前端暂时取第一个角色
                    Role = user.RoleIds.Count > 0 ? RoleService.Instance().GetRoleById(user.RoleIds[0]) : null
                };
                Users.Add(viewUser);
            }
            Count = msg.Count;
        }
    }
}
